﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace TakeoutMangesDAL
{
    public class UserInfoDAL
    {
        public static Model.UserInfo GetUserinfo(string name, string pwd)
        {
            string sqlstr = "select * from Userinfo where UserName=@name and Password=@pwd";
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",pwd)
            };
            SqlDataReader reader = DBHelper.ExecuteReader(sqlstr, System.Data.CommandType.Text, paras);
            //读取下一条数据
            if (reader.Read())
            {
                //实例化对象，存放用户数据
                UserInfo u = new UserInfo
                {
                    UserID = Convert.ToInt32(reader["UserID"]),
                    UserName = reader["UserName"].ToString(),
                    Password = Convert.ToInt32(reader["Password"]),
                };
                return u;
            }
            return null;
        }

        //注册
        public static int UserRegister(Model.UserInfo US) 
        {
            string sqlstr = "insert into UserInfo values(@UserName,@Password,@UserSex,@UserTel,@UserAdress,@Isdelete)";
            SqlParameter[] pams =new SqlParameter[]
            {
                new SqlParameter("@UserName",US.UserName),
                new SqlParameter("@Password",US.Password),
                new SqlParameter("@UserSex",US.UserSex),
                new SqlParameter("@UserTel",US.UserTel),
                new SqlParameter("@UserAdress",US.UserAdress),
                new SqlParameter("@Isdelete",US.Isdelete),
            };
            return DBHelper.ExecuteNonQuery(sqlstr,pams);
        }
        public static List<UserInfo> UserInfo()
        {
            string sqltr = "select * from UserInfo";
            SqlDataReader reader = DBHelper.ExecuteReader(sqltr, CommandType.Text, null);
            List<UserInfo> list = new List<UserInfo>();
            while (reader.Read())
            {
                UserInfo use = new UserInfo
                {
                    UserID = Convert.ToInt32(reader["UserID"]),
                    UserName = reader["UserName"].ToString(),
                    Password = Convert.ToInt32(reader["Password"].ToString()),
                    UserSex = Convert.ToInt32(reader["UserSex"]),
                    UserTel = reader["UserTel"].ToString(),
                    UserAdress = reader["UserAdress"].ToString(),
                    Isdelete = Convert.ToInt32(reader["Isdelete"]),
                };
                list.Add(use);
            }
            reader.Close();
            return list;
        }
    }
}
